﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VERKProjectHopur16.Models;
using VERKProjectHopur16.Helpers;

namespace VERKProjectHopur16.Controllers
{
    /// <summary>
    /// Controller for events.
    /// </summary>
    public class EventController : ParentController
    {
        DataRepository m_rep = new DataRepository();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AllEvent()
        {
            IEnumerable<Eventss> depAll;
            IEnumerable<Eventss> depMed;
            IEnumerable<Eventss> depPsy;
            IEnumerable<Eventss> depSos;
            String strSort = "StartDate";

            if (Session["eventOrder"] != null)
            {
                if (Session["eventOrder"].Equals(1))
                {
                    strSort = "Name";
                }
                else if (Session["eventOrder"].Equals(2))
                {
                    strSort = "StartDate";
                }
                else if (Session["eventOrder"].Equals(3))
                {
                    strSort = "EndDate";
                }
                else if (Session["eventOrder"].Equals(4))
                {
                    strSort = "Author.Name";
                }
            }

            // All events 
            var tempResult = (from events in m_rep.GetAllEvent()
                              where events.Active == true
                              select events);

            // Events filtered by department
            if (strSort == "StartDate" || strSort == "EndDate")
            {
                depAll = tempResult.AsQueryable().OrderByDescending(strSort);
                depMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                depPsy = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                depSos = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);              
            }
            else
            { 
                depAll = tempResult.AsQueryable().OrderBy(strSort);
                depMed = tempResult.Where( m => m.DepartmentID == 2 ).AsQueryable().OrderBy(strSort);
                depPsy = tempResult.Where( m => m.DepartmentID == 3 ).AsQueryable().OrderBy(strSort);
                depSos = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);            
            }

            DepEventViewModel model = new DepEventViewModel();
            model.allDept = depAll;
            model.medDept = depMed;
            model.psiDept = depPsy;
            model.socDept = depSos;

            return View(model);
        }
        public ActionResult OrderEvent(int? id)
        {
            if (id.HasValue)
            {
                Session["eventOrder"] = id;
            }
            return RedirectToAction("AllEvent");
        }

        public ActionResult SingleEvent(int? id)
        {
            if ( id.HasValue )
            {
                var event_id = id.Value;
                var result = m_rep.GetEventById(event_id);
                if ( result == null )
                {
                    return View("NotFound");
                }
                return View(result);
            }
            else
            {
                return View("NotFound");
            }
        }
    }
}
